package com.winit.project.util;

import com.winit.common.util.ConfigUtil;
import com.winit.common.util.Dom4jUtil;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 工程构建过程中使用的工具类
 * Created by wangjian on 16/10/9.
 */
public class ProjectBuildUtil {

    private static Logger logger = LoggerFactory.getLogger(ProjectBuildUtil.class);

    /**
     * 请求sonar 获取工程检查状态的地址
     * @param projectName
     * @return
     * @throws Exception
     */
    public static String getSonarUrlForProjectStatus(String projectName) throws Exception{
        String url = ConfigUtil.getValue("sonar_url");
        if(StringUtils.isEmpty(url)){
            logger.error("com.winit.project.util.ProjectBuildUtil.getSonarUrlForProjectStatus url为空");
            throw new Exception("请求sonar,url为空。请联系beetle管理员检查config配置!");
        }

        return url + "/api/qualitygates/project_status?projectKey=" + getSonarProjectKeyForWar(projectName);
    }

    /**
     * 获取工程调用sonar的projectKey
     * maven_war工程
     * @param projectName 工程名
     * @return
     * @throws Exception
     */
    public static String getSonarProjectKeyForWar(String projectName) throws Exception{
        String codePath = ProjectUtil.getProjectCodePath(projectName);
        String xmlPath = codePath + "pom.xml";

        List<Element> list = Dom4jUtil.parserXml(xmlPath);
        String artifactId = "";
        String version = "";
        for (Element element:list){
            if(!StringUtils.isEmpty(artifactId) && !StringUtils.isEmpty(version)){
                break;
            }
            String name = element.getName();
            if(!"artifactId".equals(name) && !"version".equals(name)){
                continue;
            }
            if("artifactId".equals(name)){
                artifactId = getPomValue(list,element.getTextTrim());
                continue;
            }
            if("version".equals(name)){
                version = getPomValue(list,element.getTextTrim());
                continue;
            }
        }
        if(StringUtils.isEmpty(artifactId) || StringUtils.isEmpty(version)){
            logger.error("com.winit.project.util.ProjectBuildUtil.getSonarProjectKeyForWar 存在空值.artifactId="+artifactId+",version="+version);
            throw new Exception("获取工程sonar projectKey 存在空值.artifactId="+artifactId+",version="+version);
        }
        String projectKey = artifactId + ":" + version;
        return projectKey;
    }

    private static String getPomValue(List<Element> list ,String text){
        if(!text.startsWith("$")){
            return text;
        }
        String rtn = "";
        text = text.replace("${","").replace("}","");
        for(Element element : list){
            if(!StringUtils.isEmpty(rtn)){
                break;
            }
            if(!"properties".equals(element.getName())){
                continue;
            }
            List l = element.content();
            for (Object object:l){
                if("DefaultElement".equals(object.getClass().getSimpleName())){
                    DefaultElement el = (DefaultElement) object;
                    if(text.equals(el.getName())){
                        rtn = el.getTextTrim();
                        break;
                    }
                }
            }
        }

        return rtn;
    }

    /**
     * 获取工程调用sonar的projectKey
     * php工程
     * @param projectName 工程名
     * @return
     * @throws Exception
     */
    public static String getSonarProjectKeyForPhp(String projectName) throws Exception{
        String codePath = ProjectUtil.getProjectCodePath(projectName);
        String xmlPath = codePath + "build.xml";
        String projectKey = "";
        List<Element> list = Dom4jUtil.parserXml(xmlPath);
        for (Element element:list){
            String key = element.attribute(0).getStringValue();
            if("sonar.projectKey".equals(key)){
                projectKey = element.attribute(1).getStringValue();
                break;
            }
        }
        return projectKey;
    }
}
